package com.ivc.report.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.ss.usermodel.Workbook;

import com.cs.framework.common.util.IOUtils;
import com.cs.framework.common.util.ReflectUtils;
import com.cs.framework.jdbc.common.bo.PagingInfo;
import com.cs.framework.jdbc.common.dao.CommonDao;
import com.ivc.report.service.ReportService;

/**
 * 报表抽象Service
 *
 * @author hedongzhou
 *
 */
@SuppressWarnings("all")
public abstract class AbstractReportServiceImpl implements ReportService {

	@Resource
	protected CommonDao commonDao;

	/**
	 * 获取查询SQL的ID
	 * 
	 * @return
	 */
	public abstract String getQuerySqlId();

	/**
	 * 获取报表模板
	 * 
	 * @return
	 */
	public abstract String getTemplate();

	/**
	 * 查询总数
	 * 
	 * @param params
	 * @return
	 */
	public int count(Map params) {
		return commonDao.count(getQuerySqlId(), params);
	}

	/**
	 * 查询列表
	 * 
	 * @param params
	 * @return
	 */
	public List<Map> query(Map params) {
		return commonDao.selectList(getQuerySqlId(), params, ReflectUtils.mapToBean(params, PagingInfo.class),
				Map.class);
	}

	/**
	 * 查询所有
	 * 
	 * @param params
	 * @return
	 */
	public List<Map> queryAll(Map params) {
		return commonDao.selectList(getQuerySqlId(), params, Map.class);
	}

	/**
	 * 创建报表Excel
	 * 
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public Workbook createExcel(Map params) throws Exception {
		Map info = new HashMap();
		info.put("info", queryAll(params));

		XLSTransformer former = new XLSTransformer();
		return former.transformXLS(IOUtils.loadOneResource("classpath:com/ivc/report/template/" + getTemplate())
				.getInputStream(), info);
	}

}
